package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.BussinessCommentTopAdapter;
import com.yiwei.zxp.adapter.GoodsCommentAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.GoodsCommentLabelBean;
import com.yiwei.zxp.api.bean.GoodsCommentListBean;
import com.yiwei.zxp.api.bean.SellCommentReplyBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.fragment.KeyMapDailog;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.OnLoadeMoreListener;
import com.yiwei.zxp.view.V19FrameLayout;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class GoodsCommentActivity extends BaseActivity {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list_one)
    RecyclerView listOne;
    @BindView(R.id.list_two)
    RecyclerView listTwo;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private KeyMapDailog dialog;
    private Unbinder unbiner;
    private BussinessCommentTopAdapter adapter;
    private GoodsCommentAdapter bussinessMyCommentAdapter;
    private String keyword;
    private OnLoadeMoreListener onLoadeMoreListener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getSellCommentLists(page);
        }
    };
    private String goods_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bussiness_comment);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        goods_id = getIntent().getStringExtra("goods_id");
        txtTitle.setText("全部评价");
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                onLoadeMoreListener.setPage();
                getSellCommentLists(1);
            }
        });
        adapter = new BussinessCommentTopAdapter(this);
        adapter.setOnChooseListener(new BussinessCommentTopAdapter.OnChooseListener() {
            @Override
            public void onClick(String key_word) {
                keyword = key_word;
                onLoadeMoreListener.setPage();
                getSellCommentLists(1);
            }
        });
        listOne.setLayoutManager(new GridLayoutManager(this, 4));
        listOne.setAdapter(adapter);

        bussinessMyCommentAdapter = new GoodsCommentAdapter(this);
        bussinessMyCommentAdapter.setOnChooseListener(new GoodsCommentAdapter.OnChooseListener() {
            @Override
            public void onClick(String id) {
                showComment(id);
            }
        });
        listTwo.setAdapter(bussinessMyCommentAdapter);
        getGoodsCommentLabel();
    }

    public void getGoodsCommentLabel() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        GoodsCommentLabelBean bean = new GoodsCommentLabelBean();
        bean.goods_id = goods_id;

        Api.api_service.getGoodsCommentLabel(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.update(result.datas);
                    if (result.datas != null && result.datas.size() != 0) {
                        keyword = result.datas.get(0).keyword;
                        onLoadeMoreListener.setPage();
                        getSellCommentLists(1);
                    }
                });
    }

    public void getSellCommentLists(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        GoodsCommentListBean bean = new GoodsCommentListBean();
        bean.goods_id = goods_id;
        bean.keyword = keyword;
        bean.page = page;
        Api.api_service.getGoodsCommentList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    bussinessMyCommentAdapter.add(result.datas, page == 1);
                    if (bussinessMyCommentAdapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    public void getSellCommentReply(String id, String content) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        SellCommentReplyBean bean = new SellCommentReplyBean();
        bean.id = id;
        bean.content = content;
        Api.api_service.getSellCommentReply(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "评论成功");
                    onLoadeMoreListener.setPage();
                    getSellCommentLists(1);
                });
    }


    private void showComment(String id) {
        dialog = new KeyMapDailog(new KeyMapDailog.SendBackListener() {
            @Override
            public void sendBack(final String inputText) {
                getSellCommentReply(id, inputText);
            }
        });

        dialog.show(getSupportFragmentManager(), "dialog");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
